package ac.biu.nlp.cpbuilder;

import java.io.File;
import java.util.Iterator;
import java.util.LinkedList;

import eu.excitementproject.eop.common.utilities.Utils;

/**
 * How to use:
 * 
 * Run this class from Eclipse.<BR>
 * <B>IT MUST BE RUN FROM A CLEAN WORKSPACE - WHICH CONTAINS
 * ONLY THIS PROJECT, AND NO OTHER PROJECTS!</B>
 * <P>
 * Before running this class - you should run maven install
 * on all the projects you depend on (they exist in another Eclipse project).
 * <P>
 * Then, specify these projects in the pom file: cpbuilder.pom
 * <P>
 * 
 * In the run configuration,
 * you should define the variable "dollar", with the value "$".
 * (Run configurations -> create a new "Java application" -> go to "arguments" tab ->
 * click the button "Variables..." -> define the variable).
 * <BR>
 * Your system (the OS) - and the system in which you want to run the projects
 * (i.e., your computer and the server),
 * should include the following environment variables:
 * JAVA_HOME - points to the JDK home directory
 * M2_REPO - points to the local maven repository (a path in the computer,
 * usually it is ~/.m2/repository).
 * <P>
 * Run this class with the following arguments:
 * <pre>
 * ":"
 * ${env_var:M2_REPO}
 * \${dollar}M2_REPO
 * ${env_var:JAVA_HOME}
 * \${dollar}JAVA_HOME
 * </pre>
 * 
 * @author Asher Stern
 * @since Feb 1, 2013
 *
 */
public class EclipseCP
{
	public static final String LOCAL = "cpbuilder"+File.separatorChar+"target"+File.separatorChar+"classes:";

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		try
		{
			Iterator<String> argsIterator = Utils.arrayToCollection(args, new LinkedList<String>()).iterator();
//			while (argsIterator.hasNext())
//			{
//				System.out.println(argsIterator.next());
//			}
//			System.out.println();
//			argsIterator = Utils.arrayToCollection(args, new LinkedList<String>()).iterator();

			new EclipseCP(
					argsIterator.next(),argsIterator.next(),argsIterator.next(),argsIterator.next(),argsIterator.next()
					).go();
		}
		catch(Exception e)
		{
			e.printStackTrace(System.out);
		}
	}
	
	

	



	public EclipseCP(String delimiter, String prefixMavenRepository,
			String replacementPrefixMavenRepository, String javaHome,
			String replacementJavaHome)
	{
		super();
		this.delimiter = delimiter;
		this.prefixMavenRepository = prefixMavenRepository;
		this.replacementPrefixMavenRepository = replacementPrefixMavenRepository;
		this.javaHome = javaHome;
		this.replacementJavaHome = replacementJavaHome;
	}



	public void go()
	{
		String cp = System.getProperty("java.class.path");
		cp = cp.replaceAll(File.pathSeparator, delimiter);
		cp = cp.replaceAll(prefixMavenRepository, replacementPrefixMavenRepository);
		cp = cp.replaceAll(javaHome, replacementJavaHome);
		
		
		
		int localProjectEnds = cp.indexOf(LOCAL)+LOCAL.length();
		//System.out.println(cp.substring(0, localProjectEnds));
		
		String toRemove = cp.substring(0,localProjectEnds);
		if (toRemove.indexOf(":")!=(toRemove.length()-1)) throw new RuntimeException("Problem");
		
		cp = cp.substring(localProjectEnds,cp.length());
		
		verifyNoUnrecognized(cp);
		
		System.out.println(cp);
	}
	
	private void verifyNoUnrecognized(String cp)
	{
		String[] components = cp.split(delimiter);
		for (String component : components)
		{
			if (component.startsWith("$"))
			{
				// do nothing
			}
			else
			{
				throw new RuntimeException("Unrecognized component: "+component);
			}
		}
	}
	
	private String delimiter;
	private String prefixMavenRepository;
	private String replacementPrefixMavenRepository;
	private String javaHome;
	private String replacementJavaHome;
	
}
